package com.redoor.app.stake.basicdata.service.impl;


import com.redoor.app.stake.basicdata.model.BaseCharger;
import com.redoor.app.stake.basicdata.model.constants.BasicDataRedisConstants;
import com.redoor.app.stake.basicdata.service.BaseChargerRedisService;
import com.redoor.app.stake.common.service.RedisService;
import com.redoornetwork.framework.data.redis.util.RedisUtils;
import com.yotouch.app.stake.model.Charger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;


@Service
public class BaseChargerRedisServiceImpl implements BaseChargerRedisService {
    final org.slf4j.Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    RedisService redisService;

    @Autowired
    RedisUtils redisUtils;

    @Override
    public boolean modifyChargerInfo(String key, BaseCharger charger) {
        boolean flag = false;
        try{
            if(StringUtils.isEmpty(charger.getUuid())
                    || StringUtils.isEmpty(key)
                    || !charger.getUuid().equals(key)){
                logger.error("uuid is not matching station {},{}",key, charger.getUuid());
                return false;
            }

            String realKey = BasicDataRedisConstants.REDIS_CHARGER_PRE+key;
            flag = redisService.add(realKey,charger,BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
        }catch (Exception e){
            e.printStackTrace();
        }
        return flag;
    }


    @Override
    public boolean isExistsCharger(String key) {
        return redisService.isExists(key);
    }



    @Override
    public boolean deleteChargerInfo(String key) {
        String realKey = BasicDataRedisConstants.REDIS_CHARGER_PRE+key;
        if(isExistsCharger(realKey)){
            //删除枪相关系列缓存
            redisUtils.removePattern(realKey);
        }
        return true;
    }

    @Override
    public BaseCharger getChargerFromRedis(String uuid) {
        String realKey = BasicDataRedisConstants.REDIS_CHARGER_PRE+uuid;
        BaseCharger charger = (BaseCharger)redisUtils.get(realKey);
        return charger;
    }





    @Override
    public boolean modifyChargerInfoHSF(String key, Charger charger) {
        boolean flag = false;
        try{
            if(StringUtils.isEmpty(charger.getUuid())
                    || StringUtils.isEmpty(key)
                    || !charger.getUuid().equals(key)){
                logger.error("uuid is not matching station {},{}",key, charger.getUuid());
                return false;
            }

            String realKey = BasicDataRedisConstants.REDIS_CHARGER_HSF_PRE+key;
            String noKey = BasicDataRedisConstants.REDIS_CHARGER_HSF_PRE+charger.getChargerNo();
            redisService.add(realKey,charger,BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
            redisService.add(noKey+":UUID",charger.getUuid(),BasicDataRedisConstants.REDIS_OVER_TIME_DAY);
        }catch (Exception e){
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public Charger getChargerHSF(String uuid) {
        String key = BasicDataRedisConstants.REDIS_CHARGER_HSF_PRE+uuid;
        Charger charger = (Charger)redisUtils.get(key);
        return charger;
    }

    @Override
    public String getUuidByChargerNo(String chargerNo){
        String noKey = BasicDataRedisConstants.REDIS_CHARGER_HSF_PRE+chargerNo+":UUID";
        return (String) redisUtils.get(noKey);
    }
}
